<?php

namespace app\admin\service;

use app\admin\model\SportRecord;
use think\Model;

class ClassPortraitService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new SportRecord();
    }

    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];

        $startYear = isset($param['startYear']) ? (int)$param['startYear'] : '';
        $endYear = isset($param['endYear']) ? (int)$param['endYear'] : '';
        if ($startYear && $endYear) {
            $map[] = '';
        }

        $project = isset($param['project']) ? (int)$param['project'] : '';
        if ($project) {
            $map[] = ['projectId', '=', $project];
        }

        $grade = isset($param['grade']) ? (int)$param['grade'] : '';
        if ($grade) {
            $map[] = ['gradeId', '=', $grade];
        }

        $department = isset($param['department']) ? (int)$param['department'] : '';
        if ($department) {
            $map[] = ['departmentsId', '=', $department];
        }

        // 更新时间
        $startTime = isset($param['startTime']) ? trim($param['startTime']) : '';
        $endTime = isset($param['endTime']) ? trim($param['endTime']) : '';
        if ($startTime && $endTime) {
            $map[] = ['update_time', '>= time', $startTime];
            $map[] = ['update_time', '<= time', $endTime];
        }

        $result = $this->model->where($map)->order('id desc')->group('gradeId, departmentsId, projectId')->page(
            isset($param['page']) ? $param['page'] : 1,
            isset($param['limit']) ? $param['limit'] : 20)->column("id");

        $list = [];
        if (is_array($result)) {
            foreach ($result as $val) {
                $info = $this->model->getInfo($val);
                $list[] = $info;
            }
        }

        //获取数据总数
        $count = $this->model->where($map)->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}